package com.guinea.core.acegi;

import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.springframework.dao.DataAccessException;

public class UserDetailsServiceImpl implements UserDetailsService {
	private AcegiCacheManager acegiCacheManager;

	public void setAcegiCacheManager(AcegiCacheManager acegiCacheManager) {
		this.acegiCacheManager = acegiCacheManager;
	}
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		acegiCacheManager.initUserCache();
		//当用户数量级变大后，需要先取缓存，再取数据库
//        List users = userManager.getUsersByName(username);
//        if (users == null || users.isEmpty()) {
//            throw new UsernameNotFoundException("user '" + username + "' not found...");
//        } else {
//        	User user = (User)users.get(0);
//        	GrantedAuthority[] authorities = role2authorities(user.getRoles());
//            return new org.acegisecurity.userdetails.User(user.getName(), user
//    				.getPassword(), true, true, true, true, authorities);
//        }
		
		return acegiCacheManager.getUserCache().getUserFromCache(username);
	}

}
